package com.xyz.rok.tools.controller;

import com.xyz.rok.tools.model.ApplyTitle;
import com.xyz.rok.tools.model.Result;
import com.xyz.rok.tools.model.Title;
import com.xyz.rok.tools.model.UnionMember;
import com.xyz.rok.tools.model.request.TitleRequest;
import com.xyz.rok.tools.model.request.UnionRequest;
import com.xyz.rok.tools.model.response.QueueInfoResponse;
import com.xyz.rok.tools.service.TitleQueueService;
import com.xyz.rok.tools.service.TitleService;
import com.xyz.rok.tools.service.UnionMemberService;
import com.xyz.rok.tools.service.UnionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author ZhangYiXin
 * @version 1.0
 * @date 2021/5/12 14:24
 **/
@Slf4j
@RestController
@RequestMapping("/api/title")
public class TitleController {
    @Autowired
    private UnionService unionService;
    @Autowired
    private UnionMemberService unionMemberService;
    @Autowired
    private TitleService titleService;
    @Autowired
    private TitleQueueService titleQueueService;

    @GetMapping("/getApplyingTitle")
    public Result getApplyingTitle(HttpServletRequest request) {
        return Result.success(titleService.selectByUserId((Long) request.getAttribute("userId")));
    }

    @GetMapping("/getApplyingByServerId")
    public Result getApplyingByServerId(@RequestParam("serverId") Integer serverId, @RequestParam("title") Integer title) {
        return Result.success(titleService.getFirstTitle(title, serverId));
    }

    @GetMapping("/getCountByServerId")
    public Result getApplyingByServerId(@RequestParam("serverId") Integer serverId) {
        return Result.success(titleService.getCountByServerId(serverId));
    }

    @GetMapping("/updateGetTitleTime")
    public Result updateGetTitleTime(@RequestParam("id") Long id) throws Exception {
        return Result.success(titleService.updateGetTitleTime(id));
    }

    @GetMapping("/queueInfo")
    public Result queueInfo(HttpServletRequest request, @RequestParam("serverId") Integer serverId) {
        Long userId = (Long) request.getAttribute("userId");
        List<ApplyTitle> applyTitles = titleService.selectByServerId(serverId);
        Map<Integer, Integer> queue = new HashMap(8);
        queue.put(0, 0);
        queue.put(1, 0);
        queue.put(2, 0);
        Integer index = null;
        Integer title = null;
        for (ApplyTitle applyTitle : applyTitles) {
            Integer count = queue.get(applyTitle.getTitle());
            queue.put(applyTitle.getTitle(), count + 1);
            if (applyTitle.getUserId() == userId) {
                title = applyTitle.getTitle();
                index = queue.get(applyTitle.getTitle());
            }
        }
        List<QueueInfoResponse> queueInfoResponseList = new ArrayList<>();
        QueueInfoResponse queueInfoResponse = null;
        for (Map.Entry<Integer, Integer> integerIntegerEntry : queue.entrySet()) {
            queueInfoResponse = new QueueInfoResponse();
            queueInfoResponse.setTitle(integerIntegerEntry.getKey());
            queueInfoResponse.setCount(integerIntegerEntry.getValue());
            if (integerIntegerEntry.getKey().equals(title)) {
                queueInfoResponse.setIndex(index);
            }
            queueInfoResponseList.add(queueInfoResponse);
        }
        return Result.success(queueInfoResponseList);
    }

    @PostMapping("/applyTitle")
    @ResponseBody
    public Result applyTitle(@RequestBody TitleRequest titleRequest) throws Exception {
        titleService.applyTile(titleRequest);
        return Result.success();
    }

    @PostMapping("/checkingTitle")
    @ResponseBody
    public Result checkingTitle(@RequestBody TitleRequest titleRequest) throws Exception {
        titleService.checkingTitle(titleRequest);
        return Result.success();
    }

    @PostMapping("/checkingTitleAdmin")
    @ResponseBody
    public Result checkingTitleAdmin(@RequestBody TitleRequest titleRequest) throws Exception {
        titleService.checkingTitleAdmin(titleRequest);
        return Result.success();
    }

    @GetMapping("/selectTitleByServerId")
    @ResponseBody
    public Result selectTitleByServerId(@RequestParam("serverId") Integer serverId) throws Exception {
        return Result.success(titleService.selectTitleByServerId(serverId));
    }

    @GetMapping("/selectBySerialNumber")
    @ResponseBody
    public Result selectBySerialNumber(@RequestParam("serialNumber") String serialNumber) throws Exception {
        return Result.success(titleService.selectBySerialNumber(serialNumber));
    }

    @PostMapping("/updateTitleByServerId")
    @ResponseBody
    public Result updateTitleByServerId(@RequestBody TitleRequest titleRequest) throws Exception {
        Title title = new Title();
        BeanUtils.copyProperties(titleRequest, title);
        titleService.updateTitleByServerId(title);
        return Result.success();
    }
}
